
from readgenome import readGenomicSequence

class GenomeReader:
    def __init__(self, chr, leftPad=1000, rightPad=5000):
        self.chr = chr
        self.buffer = None
        self.minPos = None
        self.maxPos = None
        self.leftPad = leftPad
        self.rightPad = rightPad
    
    def adjustBuffer(self, minPos, maxPos):
        # update buffer to include the range [minPos, maxPos]
        if self.buffer and minPos >= self.minPos and maxPos <= self.maxPos:
            return
        self.minPos = minPos - self.leftPad
        self.maxPos = maxPos + self.rightPad
        self.buffer = readGenomicSequence((self.chr, self.minPos, self.maxPos))

    def __getitem__(self, pos):
        self.adjustBuffer(pos, pos)
        return self.buffer[pos - self.minPos]
    
    def __getslice__(self, minPos, maxPos):
        self.adjustBuffer(minPos, maxPos)
        return self.buffer[minPos - self.minPos : maxPos - self.minPos]

